﻿using System;
using UnityEngine;

namespace UnityStandardAssets.ImageEffects
{
	// Token: 0x020005EB RID: 1515
	[ExecuteInEditMode]
	[RequireComponent(typeof(Camera))]
	[AddComponentMenu("Image Effects/Noise/Noise and Scratches")]
	public class NoiseAndScratches : MonoBehaviour
	{
		// Token: 0x06001DC6 RID: 7622 RVA: 0x0006BEA4 File Offset: 0x0006A0A4
		public void Start()
		{
			if (!SystemInfo.supportsImageEffects)
			{
				base.enabled = false;
				return;
			}
			if (this.shaderRGB == null || this.shaderYUV == null)
			{
				Debug.Log("Noise shaders are not set up! Disabling noise effect.");
				base.enabled = false;
			}
			else if (!this.shaderRGB.isSupported)
			{
				base.enabled = false;
			}
			else if (!this.shaderYUV.isSupported)
			{
				this.rgbFallback = true;
			}
		}

		// Token: 0x17000231 RID: 561
		// (get) Token: 0x06001DC7 RID: 7623 RVA: 0x0006BF30 File Offset: 0x0006A130
		public Material material
		{
			get
			{
				if (this.m_MaterialRGB == null)
				{
					this.m_MaterialRGB = new Material(this.shaderRGB);
					this.m_MaterialRGB.hideFlags = HideFlags.HideAndDontSave;
				}
				if (this.m_MaterialYUV == null && !this.rgbFallback)
				{
					this.m_MaterialYUV = new Material(this.shaderYUV);
					this.m_MaterialYUV.hideFlags = HideFlags.HideAndDontSave;
				}
				return (this.rgbFallback || this.monochrome) ? this.m_MaterialRGB : this.m_MaterialYUV;
			}
		}

		// Token: 0x06001DC8 RID: 7624 RVA: 0x00012CEC File Offset: 0x00010EEC
		public void OnDisable()
		{
			if (this.m_MaterialRGB)
			{
				UnityEngine.Object.DestroyImmediate(this.m_MaterialRGB);
			}
			if (this.m_MaterialYUV)
			{
				UnityEngine.Object.DestroyImmediate(this.m_MaterialYUV);
			}
		}

		// Token: 0x06001DC9 RID: 7625 RVA: 0x0006BFD0 File Offset: 0x0006A1D0
		public void SanitizeParameters()
		{
			this.grainIntensityMin = Mathf.Clamp(this.grainIntensityMin, 0f, 5f);
			this.grainIntensityMax = Mathf.Clamp(this.grainIntensityMax, 0f, 5f);
			this.scratchIntensityMin = Mathf.Clamp(this.scratchIntensityMin, 0f, 5f);
			this.scratchIntensityMax = Mathf.Clamp(this.scratchIntensityMax, 0f, 5f);
			this.scratchFPS = Mathf.Clamp(this.scratchFPS, 1f, 30f);
			this.scratchJitter = Mathf.Clamp(this.scratchJitter, 0f, 1f);
			this.grainSize = Mathf.Clamp(this.grainSize, 0.1f, 50f);
		}

		// Token: 0x06001DCA RID: 7626 RVA: 0x0006C09C File Offset: 0x0006A29C
		public void OnRenderImage(RenderTexture source, RenderTexture destination)
		{
			this.SanitizeParameters();
			if (this.scratchTimeLeft <= 0f)
			{
				this.scratchTimeLeft = UnityEngine.Random.value * 2f / this.scratchFPS;
				this.scratchX = UnityEngine.Random.value;
				this.scratchY = UnityEngine.Random.value;
			}
			this.scratchTimeLeft -= Time.deltaTime;
			Material material = this.material;
			material.SetTexture("_GrainTex", this.grainTexture);
			material.SetTexture("_ScratchTex", this.scratchTexture);
			float num = 1f / this.grainSize;
			material.SetVector("_GrainOffsetScale", new Vector4(UnityEngine.Random.value, UnityEngine.Random.value, (float)Screen.width / (float)this.grainTexture.width * num, (float)Screen.height / (float)this.grainTexture.height * num));
			material.SetVector("_ScratchOffsetScale", new Vector4(this.scratchX + UnityEngine.Random.value * this.scratchJitter, this.scratchY + UnityEngine.Random.value * this.scratchJitter, (float)Screen.width / (float)this.scratchTexture.width, (float)Screen.height / (float)this.scratchTexture.height));
			material.SetVector("_Intensity", new Vector4(UnityEngine.Random.Range(this.grainIntensityMin, this.grainIntensityMax), UnityEngine.Random.Range(this.scratchIntensityMin, this.scratchIntensityMax), 0f, 0f));
			Graphics.Blit(source, destination, material);
		}

		// Token: 0x040017B1 RID: 6065
		public bool monochrome = true;

		// Token: 0x040017B2 RID: 6066
		public bool rgbFallback;

		// Token: 0x040017B3 RID: 6067
		[Range(0f, 5f)]
		public float grainIntensityMin = 0.1f;

		// Token: 0x040017B4 RID: 6068
		[Range(0f, 5f)]
		public float grainIntensityMax = 0.2f;

		// Token: 0x040017B5 RID: 6069
		[Range(0.1f, 50f)]
		public float grainSize = 2f;

		// Token: 0x040017B6 RID: 6070
		[Range(0f, 5f)]
		public float scratchIntensityMin = 0.05f;

		// Token: 0x040017B7 RID: 6071
		[Range(0f, 5f)]
		public float scratchIntensityMax = 0.25f;

		// Token: 0x040017B8 RID: 6072
		[Range(1f, 30f)]
		public float scratchFPS = 10f;

		// Token: 0x040017B9 RID: 6073
		[Range(0f, 1f)]
		public float scratchJitter = 0.01f;

		// Token: 0x040017BA RID: 6074
		public Texture grainTexture;

		// Token: 0x040017BB RID: 6075
		public Texture scratchTexture;

		// Token: 0x040017BC RID: 6076
		public Shader shaderRGB;

		// Token: 0x040017BD RID: 6077
		public Shader shaderYUV;

		// Token: 0x040017BE RID: 6078
		public Material m_MaterialRGB;

		// Token: 0x040017BF RID: 6079
		public Material m_MaterialYUV;

		// Token: 0x040017C0 RID: 6080
		public float scratchTimeLeft;

		// Token: 0x040017C1 RID: 6081
		public float scratchX;

		// Token: 0x040017C2 RID: 6082
		public float scratchY;
	}
}
